﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Marmat.Forms
{
    public sealed class WindowsAPI
    {
        /// <summary>锁定窗口(控件)句柄,调用此函数后,必须要解锁</summary>
    /// <param name="hwndLock">窗口(控件)句柄</param>
        [DllImport("user32")]
        public static extern int LockWindowUpdate(int hwndLock);

        /// <summary>
    /// 禁止窗体中的绘制操作
    /// </summary>
    /// <param name="hWnd"></param>
    /// <param name="wMsg"></param>
    /// <param name="wParam"></param>
    /// <param name="lParam"></param>
    /// <returns></returns>
    /// <remarks></remarks>
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);

        private const int WM_SETREDRAW = 11;

        /// 禁止窗体中的绘制操作
        public static void SuspendDrawing(Control parent)
        {
            SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
        }

        /// <summary>
    /// 恢复窗体中的绘制操作
    /// </summary>
    /// <param name="parent"></param>
    /// <remarks></remarks>
        public static void ResumeDrawing(Control parent)
        {
            SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
            parent.Refresh();
        }

        /// <summary>
    /// 使用选定的刷子、源位图和ROP3码绘制选定的矩形
    /// 获得屏幕图形并将它写入内存中的一个位图中(截屏)
    /// </summary>
    /// <param name="hdcDest">目的上下文设备的句柄 </param>
    /// <param name="nXDest">目的图形的左上角的x坐标 </param>
    /// <param name="nYDest">目的图形的左上角的y坐标 </param>
    /// <param name="nWidth">目的图形的矩形宽度 </param>
    /// <param name="nHeight">目的图形的矩形高度 </param>
    /// <param name="hdcSrc">源上下文设备的句柄</param>
    /// <param name="nXSrc">源图形的左上角的x坐标</param>
    /// <param name="nYSrc">源图形的左上角的x坐标</param>
    /// <param name="dwRop">光栅操作代码 </param>
    /// <returns></returns>
        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    }
}